package com.zhuoyue.common.utils;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
 * @author: DaDing
 * @description:
 * @create: 2018-11-09 16:18
 **/
public class AESUtils {

    private static final String KEY = "16zhijiashenghuo";

    //加密
    public static String encrypt(String content) {
        final Base64.Encoder encoder = Base64.getEncoder();
        // 创建AES秘钥
        SecretKeySpec key = new SecretKeySpec(KEY.getBytes(), "AES");
        // 创建密码器
        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
            // 初始化加密器
            byte[] initParam = KEY.getBytes();
            IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
            cipher.init(Cipher.ENCRYPT_MODE, key, ivParameterSpec);
            // 加密
            byte[] bytes = cipher.doFinal(content.getBytes("UTF-8"));
            // 同样对加密后数据进行 base64 编码
            String encodeToString = encoder.encodeToString(bytes);
            return encodeToString.replace("\n", "");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }

        return null;

    }


    //揭秘
    public static String decrypt(String content) {
        final Base64.Decoder decoder = Base64.getDecoder();
        // 创建AES秘钥
        SecretKeySpec key = new SecretKeySpec(KEY.getBytes(), "AES");
        // 创建密码器
        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
            byte[] initParam = KEY.getBytes();
            IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
            // 初始化解密器
            cipher.init(Cipher.DECRYPT_MODE, key, ivParameterSpec);
            byte[] decode = decoder.decode(content);
            return new String(cipher.doFinal(decode));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }
        // 解密
        return null;
    }

    public static void main(String[] args) {
        String decrypt = encrypt("15993001086");
        System.out.printf(decrypt);
    }

}
